#include "RtScriptJob.h"

RtScriptJob::RtScriptJob(const QString& name) : RtJob(name)
{
	engine_ = new RtScriptEngine(this); // here ?
}

RtScriptJob::~RtScriptJob(void)
{
}
bool RtScriptJob::arm_()
{
	disarm_();

	if (code_.isEmpty()) return false;

	// engine_ = new RtScriptEngine(this); // or here?
	program_ = QScriptProgram(code_,objectName() + "_code");
	if (!engine_->canEvaluate(code_))
	{
		throwScriptError("Error in script code.");
		disarm_();
		return false;
	}

	return RtJob::arm_();
}
void RtScriptJob::disarm_()
{
	RtJob::disarm_();
}
void RtScriptJob::run()
{
	QString msg;
	if (!engine_->evaluate(program_,msg))
	{
		disarm_();
		pushError("Error executing script job",msg);
	}
}
void RtScriptJob::setCode(const QString& cd)
{
	if (cd.isEmpty() || cd==code_) return;

	{
		bool onlineChange = armed_;
		if (onlineChange)
		{
			jobLock();
			disarm_();
		}
		code_ = cd;
		if (onlineChange)
		{
			arm_();
			jobUnlock();
		}
		emit propertiesChanged();
	}
}


